package br.com.localfloripa.controllers;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;

import br.com.localfloripa.dao.RamoDao;
import br.com.localfloripa.entities.Ramo;

@Named("ramoController")
@Scope("session")
public class RamoController implements Serializable {
	
	private static final long serialVersionUID = -74508818729891213L;
	private static final Logger log = LoggerFactory.getLogger(RamoController.class);
	private final Integer pageSize = 10;
	
	@Inject
	@Named("ramoDao")
	private RamoDao ramoDao; 
	
	private LazyDataModel<Ramo> lazyModelRamo;
	private Ramo ramo;
	
	/** controla linha selecionada do dataTable **/
	private boolean selectedRamo = false;
		
	public RamoController() {
		
	}
	
	@PostConstruct
	public void loadModels() {
		
		lazyModelRamo = new LazyDataModel<Ramo>() {

			@Override
			public int getPageSize() {
				return pageSize;
			}

			@Override
			public int getRowCount() {
				return ramoDao.countListRamos().intValue();
			}

			@Override
			public List<Ramo> load(int first, int pageSize, String sortField, SortOrder order, Map<String, String> filters) {
				List<Ramo> ramos = null;
				try {
					ramos = ramoDao.listRamosPaginada(first, pageSize);
				} catch (Exception e) {
					log.error(e.getMessage(),e);
				}
				return ramos;
			}
			
			@Override  
		    public Object getRowKey(Ramo ramo) {  
		        return ramo.getId();  
		    } 
			
			@Override  
		    public Ramo getRowData(String rowKey) {  
				
				if (rowKey != null && !rowKey.equals("null")) {
					List<Ramo> ramos = (List<Ramo>) getWrappedData();  
			          
			        for(Ramo ramo : ramos) {  
			            if(ramo.getId().equals(Long.valueOf(rowKey)))  
			                return ramo;  
			        }
				}  		          
		        return null;  
		    }  
		};
	}
	
	public String save() {
		ResourceBundle bundle = ResourceBundle.getBundle("messages.MessageBundle");
		try {
			ramoDao.persist(this.ramo);
			loadModels();
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
					bundle.getString("messages.sumary.info.confirm"), bundle.getString("manager.ramo.create.confirm")));  
			return "/faces/manager/xhtml/ramo/list.xhtml";
		} catch (Exception e) {
			log.error(e.getMessage());
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
					bundle.getString("messages.sumary.error"), bundle.getString("manager.ramo.create.error"))); 
			return "/faces/manager/xhtml/ramo/create.xhtml";
		}
	}
	
	public String update() {
		ResourceBundle bundle = ResourceBundle.getBundle("messages.MessageBundle");
		try {
			ramoDao.merge(this.ramo);
			loadModels();
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
					bundle.getString("messages.sumary.info.confirm"), bundle.getString("manager.ramo.update.confirm")));  
			return "/faces/manager/xhtml/ramo/list.xhtml";
		} catch (Exception e) {
			log.error(e.getMessage());
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
					bundle.getString("messages.sumary.error"), bundle.getString("manager.ramo.update.error"))); 
			return "/faces/manager/xhtml/ramo/update.xhtml";
		}
	}
	
	public String edit() {
		if (this.ramo != null) {
			return "/faces/manager/xhtml/ramo/update.xhtml";
		} else {
			ResourceBundle bundle = ResourceBundle.getBundle("messages.MessageBundle");
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
					bundle.getString("messages.sumary.error"), bundle.getString("manager.ramo.edit.error")));
			return "/faces/manager/xhtml/ramo/list.xhtml";
		}
	}
	
	public String create() {
		this.ramo = new Ramo();
		return "/faces/manager/xhtml/ramo/create.xhtml";
	}

	public LazyDataModel<Ramo> getLazyModelRamo() {
		return lazyModelRamo;
	}

	public Ramo getRamo() {
		if (this.ramo == null) {
			this.ramo = new Ramo();
		}
		return this.ramo;
	}

	public void setRamo(Ramo ramo) {
		this.ramo = ramo;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public boolean isSelectedRamo() {
		return selectedRamo;
	}
	
	public void onSelectedRow() {
		this.selectedRamo =  true; 
	}
	
	public void onPaged() {
		this.selectedRamo = false;
	}
		
}
